/**
 * 
 */
package com.csair.youth.web.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.csair.youth.database.Category;
import com.csair.youth.service.impl.CategoryServiceImpl;

/**
 * @author mickey
 *
 */
public class CategoryEditAction extends _BaseAction {
	private Category item;
	private CategoryServiceImpl service;
//	private TreeNode tree;
	private String treeString;

	public Category getItem() {
		return item;
	}
	public void setItem(Category item) {
		this.item = item;
	}
	
	public CategoryServiceImpl getService() {
		return service;
	}
	public void setService(CategoryServiceImpl service) {
		this.service = service;
	}

	public String getTreeString() {
		return treeString;
	}
	public void setTreeString(String treeString) {
		this.treeString = treeString;
	}
	
	public String execute(){
//		treeString = service.buildCategoryTreeString(service.buildCategoryTree(item != null?item.getId():null, true), "categoryTree", "javascript:setCategory(this, \"categoryTree\");");
		treeString = service.buildCategoryTreeString(service.buildCategoryTree(null, false).get("ROOT"), "categoryTree", "javascript:setCategory(this, \"categoryTree\");");
		if(item != null && !StringUtils.isEmpty(item.getId())){
			//从数据库获取...
			item = service.getCategory(item.getId());
		}
		else item = new Category();
		return SUCCESS;
	}
	
	public String save(){
		if(item == null) {
			this.setMessage("分类对象为空.");
			treeString = service.buildCategoryTreeString(service.buildCategoryTree(null, false).get("ROOT"), "categoryTree", "javascript:setCategory(this, \"categoryTree\");");
			return INPUT;
		}
		if(!validInput(item)) return INPUT;
		//如果父分类是本身或者其子分类，报错
		if(null == service.getCategory(item.getParentId())){
			this.setMessage("父分类不存在!");
			treeString = service.buildCategoryTreeString(service.buildCategoryTree(null, false).get("ROOT"), "categoryTree", "javascript:setCategory(this, \"categoryTree\");");
			return INPUT;
		}
		if(service.isParentSelectedSelfOrChild(item.getParentId(), item.getId())){
			this.setMessage("父分类不能选本身及其子分类!");
			treeString = service.buildCategoryTreeString(service.buildCategoryTree(null, false).get("ROOT"), "categoryTree", "javascript:setCategory(this, \"categoryTree\");");
			return INPUT;
		}
		//replace the category name's illeagle chars
		item.setName(service.replaceBanChars(item.getName()));
		service.saveCategory(item);
		this.setMessage("已成功保存分类.");
		return execute();
	}
	
	private boolean validInput(Category item){
		if(item == null) return false;
		boolean allValid = true;
		StringBuffer msgBf = new StringBuffer();
		String name = item.getName();
		if(StringUtils.isEmpty(name) || StringUtils.length(name)> 50){
			msgBf.append("分类名不能为空且必须在50个字符内<br/>");
			allValid = false;
		}
		String desc = item.getDescription();
		if(!StringUtils.isEmpty(desc) && StringUtils.length(desc)> 120){
			msgBf.append("分类描述必须在120个字符内<br/>");
			allValid = false;
		}
		String parent = item.getParentId();
		if(StringUtils.isEmpty(parent)){
			msgBf.append("父分类不能为空<br/>");
			allValid = false;
		}
		this.setMessage(msgBf.toString());
		return allValid;
	}
}
